package com.aishua.aishuawallet.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aishua.aishuawallet.Constant;
import com.aishua.aishuawallet.R;
import com.aishua.aishuawallet.activity.BaseActivity;
import com.aishua.aishuawallet.activity.ReceivablesActivity;
import com.aishua.aishuawallet.bean.Header;
import com.aishua.aishuawallet.bean.ResAuthBean;
import com.aishua.aishuawallet.bean.TokenInfo;
import com.aishua.aishuawallet.utils.LogUtils;
import com.aishua.aishuawallet.web3jmanager.Web3jManager;

import org.spongycastle.jcajce.provider.symmetric.Serpent;
import org.web3j.protocol.Web3j;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * Created by aishua on 2018/4/3.
 */

public class AccountAdapter extends RecyclerView.Adapter<AccountAdapter.MyDeviceViewHoler> {
    private static final String TAG = "AccountAdapter-";
    private TextView tv_assets;

    private Context mContext;
    private BaseActivity baseActivity;
    private ResAuthBean resAuthBean;
    private  String tokenaddr;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            String balace1 = (String) msg.obj;

            String balace =mContext.getResources().getString(R.string.totalmoney) +balace1+mContext.getResources().getString(R.string.yuan );
            tv_assets.setText(balace);
        }
    };


    public AccountAdapter(Context mContext, ResAuthBean resAuthBean) {

        this.mContext = mContext;
        this.baseActivity = (BaseActivity) mContext;
        this.resAuthBean = resAuthBean;
    }

    @Override
    public MyDeviceViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.view_account, parent, false);
        return new MyDeviceViewHoler(view);

    }

    @Override
    public void onBindViewHolder(MyDeviceViewHoler holder, final int position) {

        holder.displayView(position);
//        holder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                onRecycleViewClickListener.onRecycleViewClick(position);
//            }
//        });

    }

    @Override
    public int getItemCount() {

        if (null == resAuthBean ) {

            return 0;
        }
        LogUtils.v(TAG+"num:"+Integer.parseInt(resAuthBean.getTokenNum()));
        return Integer.parseInt(resAuthBean.getTokenNum());

//        return  3;
    }

    public class MyDeviceViewHoler extends RecyclerView.ViewHolder {

        private ImageView img_account;
        private ImageView img_code;
        private TextView tv_account;
        private TextView tv_token;
        private LinearLayout ll_account;

        public MyDeviceViewHoler(View itemView) {
            super(itemView);
            img_account = itemView.findViewById(R.id.img_account);
            img_code = itemView.findViewById(R.id.img_code);
            tv_account = itemView.findViewById(R.id.tv_account);
            tv_assets = itemView.findViewById(R.id.tv_assets);
            tv_token = itemView.findViewById(R.id.tv_token);
            ll_account = itemView.findViewById(R.id.ll_account);
        }

        public void displayView(int position) {

            TokenInfo tokenInfo = resAuthBean.getTokenInfo().get(position);
            tokenaddr = tokenInfo.getTokenAddr();
            String tokenname = tokenInfo.getTokenName();
            tv_account.setText(tokenname);
            tv_token.setText(Constant.ADDRESS_OWN);
            new Thread(){
                @Override
                public void run() {

                   String balace = getBalace(tokenaddr) ;
                   Message message = mHandler.obtainMessage();
                   message.what =1;
                   message.obj = balace;
                   mHandler.sendMessage(message);

                }
            }.start();

            ll_account.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                     if (Constant.ISTOURIST){


                         baseActivity.touristMode();

                     }else {

                         Intent intent = new Intent(mContext, ReceivablesActivity.class);
                         intent.putExtra("tokenaddr",tokenaddr);
                         mContext.startActivity(intent);
                     }

                }
            });
        }


    }


    public void setonRecycleViewClickListener(onRecycleViewClickListener onRccycleViewLongClickListener) {
        this.onRecycleViewClickListener = onRccycleViewLongClickListener;
    }

    public void setOnRccycleViewLongClickListener(onRccycleViewLongClickListener onRccycleViewLongClickListener) {

        this.onRccycleViewLongClickListener = onRccycleViewLongClickListener;
    }

    private onRecycleViewClickListener onRecycleViewClickListener;
    private onRccycleViewLongClickListener onRccycleViewLongClickListener;

    public interface onRecycleViewClickListener {

        public void onRecycleViewClick(int position);
    }

    public interface onRccycleViewLongClickListener {

        public void onRccycleViewLongClick();
    }

   public String  getBalace(String address){

       String balace = "0";
       Web3jManager web3jManager = Web3jManager.getIntance();
       try {

            balace = String.format("%.1f",web3jManager.getBalance(address) *1900);

       } catch (ExecutionException e) {

           e.printStackTrace();
       } catch (InterruptedException e) {

           e.printStackTrace();
       }
     return balace;

   }



}